package cn.wolfcode.p2p.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.MoneyWithdraw;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.domain.SystemAccount;
import cn.wolfcode.p2p.business.domain.SystemAccountFlow;
import cn.wolfcode.p2p.business.mapper.SystemAccountFlowMapper;
import cn.wolfcode.p2p.business.mapper.SystemAccountMapper;
import cn.wolfcode.p2p.business.service.ISystemAccountService;

@Service
@Transactional
public class SystemAccountServiceImpl implements ISystemAccountService {

	@Autowired
	private SystemAccountFlowMapper flowMapper;

	@Autowired
	private SystemAccountMapper accountMapper;

	/**
	 * 修改系统账户,涉及到乐观锁问题
	 * 
	 * @param account
	 */
	private void update(SystemAccount account) {
		int ret = this.accountMapper.updateByPrimaryKey(account);
		if (ret == 0) {
			System.out.println("系统账户乐观锁异常");
			throw new DisplayableException("系统繁忙,请重试");
		}
	}

	@Override
	public void chargeManageFee(BidRequest br, BigDecimal manageFee) {
		// 得到系统账户
		SystemAccount current = this.accountMapper.selectCurrent();
		// 可用余额增加
		current.setUsableAmount(current.getUsableAmount().add(manageFee));
		// 创建流水,并设置属性
		SystemAccountFlow flow = new SystemAccountFlow();
		flow.setActionTime(new Date());
		flow.setActionType(Constants.SYSTEM_ACCOUNT_ACTIONTYPE_MANAGE_CHARGE);
		flow.setAmount(manageFee);
		flow.setFreezeAmount(current.getFreezedAmount());
		flow.setNote("借款:" + br.getTitle() + "成功,收取借款手续费");
		flow.setUsableAmount(current.getUsableAmount());
		this.flowMapper.insert(flow);
		this.update(current);
	}

	@Override
	public void chargeWithdrawFee(MoneyWithdraw mw) {
		SystemAccount current = this.accountMapper.selectCurrent();
		current.setUsableAmount(current.getUsableAmount().add(mw.getFee()));
		this.update(current);

		SystemAccountFlow flow = new SystemAccountFlow();
		flow.setActionTime(new Date());
		flow.setActionType(Constants.SYSTEM_ACCOUNT_ACTIONTYPE_WITHDRAW_MANAGE_CHARGE);
		flow.setAmount(mw.getFee());
		flow.setFreezeAmount(current.getFreezedAmount());
		flow.setNote("用户:" + mw.getApplier().getUsername() + "提现" + mw.getMoneyAmount() + "成功,收取提现手续费" + mw.getFee());
		flow.setUsableAmount(current.getUsableAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void chargeInterestFee(PaymentScheduleDetail psd, BigDecimal interestFee) {
		SystemAccount current = this.accountMapper.selectCurrent();
		current.setUsableAmount(current.getUsableAmount().add(interestFee));
		this.update(current);

		SystemAccountFlow flow = new SystemAccountFlow();
		flow.setActionTime(new Date());
		flow.setActionType(Constants.SYSTEM_ACCOUNT_ACTIONTYPE_INTREST_MANAGE_CHARGE);
		flow.setAmount(interestFee);
		flow.setFreezeAmount(current.getFreezedAmount());
		flow.setNote("收到利息管理费" + interestFee);
		flow.setUsableAmount(current.getUsableAmount());
		this.flowMapper.insert(flow);
	}

}
